﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HalconDotNet;
using System.Reflection;
using System.Net.Sockets;
using System.Net;

namespace HalconAssit
{



    public interface IHalconAssit
    {
    }
    public static class HalAssit
    {
        static string path = Environment.CurrentDirectory + "/tmp/";
        static int port = 57572;
        public static void Init()
        {
            port = 57572;

        }
        /// <summary>
        /// 和调试服务端交互的PORT，IP固定为127.0.0.1
        /// </summary>
        /// <param name="Port"></param>
        public static void Init(int Port)
        {

            port = Port;
        }

        public static void Display(HObject obj, string objName)
        {
            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                //var objname = objName+obj.GetHashCode().ToString();
                var objname = objName;
                string filename = path + objname + ".hobj";
                HOperatorSet.WriteObject(obj, filename);
                NotifyServer(filename, 0);
            }
            catch (Exception ex)
            {


            }

        }
        public static void Display(HTuple obj, string htupName)
        {


            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                var objname = obj.GetHashCode().ToString();
                if (HtupleIs3DModel(obj))
                {
                    if (obj.Length > 1)
                    {
                        throw new Exception("暂时不支持多3D模型查看");
                    }


                    //count_object_model_3d_points 通过此方法进一步确定是否是3D模型数据
                    string filename = path + htupName + ".om3";
                    HOperatorSet.WriteObjectModel3d(obj, "om3", filename, new HTuple(), new HTuple());
                    NotifyServer(filename, 2);
                }
                else
                {
                    string filename = path + htupName + ".tup";
                    HOperatorSet.WriteTuple(obj, filename);
                    NotifyServer(filename, 1);

                }


            }
            catch (Exception ex)
            {
                throw new Exception("Display Error HTuple", ex);
            }


        }


        public static void NotifyServer(string fname, int type)
        {
            try
            {
                string cmd = fname + "," + type.ToString() + ";";
                TcpClient tcp = new TcpClient();
                IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
                tcp.Connect(iPEndPoint);
                StreamWriter sw = new StreamWriter(tcp.GetStream());
                sw.Write(cmd);
                sw.Flush();
                tcp.Close();
            }
            catch (Exception ex)
            {

            }

        }

        //确定HTuple 是否是 3D 模型
        public static bool HtupleIs3DModel(HTuple obj)
        {
            bool res = false;
            try
            {
                if (obj.Type == HTupleType.HANDLE)
                {
                    HTuple nums;
                    HOperatorSet.GetObjectModel3dParams(obj, "num_points", out nums);
                    if (nums.Length > 0 && nums[0] > 0)
                    {
                        res = true;
                    }
                }
            }
            catch
            {
                res = false;
            }

            return res;
        }

    }
}
